package com.ajie.ft_network;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.ajie.ft_network.baseurl.MutilBaseUrlActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * 结论： 字签证书 如果不忽略校验 不能请求 onResponse 2: Bad Request
 *     This combination of host and port requires TLS.
 * @see MainActivity#httpsRequtestVerify(android.view.View)
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void baseUrl(View view) {
        startActivity(new Intent(this, MutilBaseUrlActivity.class));
    }

    public void httpRequtest(View view) {
        OkHttpClient client = new OkHttpClient.Builder().build();

        client.newCall(new Request.Builder().url("http://192.168.88.57:8443/hello").build())
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: ",e );
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.i(TAG, "onResponse: 1");
                        if (response != null ) {
                            ResponseBody body = response.body();
                            if (body != null) {
                                String string = body.string();
                                Log.i(TAG, "onResponse 2: "+string);
                            }
                        }
                    }
                });
    }


    public void httpsRequtestVerify(View view) {
        OkHttpClient client = new OkHttpClient.Builder().build();

        client.newCall(new Request.Builder().url("http://192.168.88.57:8443/hello").build())
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: ",e );
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.i(TAG, "onResponse: 1");
                        if (response != null ) {
                            ResponseBody body = response.body();
                            if (body != null) {
                                String string = body.string();
                                Log.i(TAG, "onResponse 2: "+string);
                            }
                        }
                    }
                });
    }

    public void httpsRequtestNotVerify(View view) {
        OkHttpClient client = (OkHttpClient) HttpClientUtil.getUnsafeOkHttpClient();
        client.newCall(new Request.Builder().url("https://192.168.88.57:8443/hello").build())
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: ",e );
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.i(TAG, "onResponse: 1");
                        if (response != null ) {
                            ResponseBody body = response.body();
                            if (body != null) {
                                String string = body.string();
                                Log.i(TAG, "onResponse 2: "+string);
                            }
                        }
                    }
                });
    }
}